/*
 * @Descripttion: 用户分组管理功能
 * @Author: YuZhenJing
 * @Date: 2020-01-10 19:55:22
 * @LastEditors: YuZhenJing
 * @LastEditTime: 2020-03-20 15:34:58
 */

/**
 * 查询用户分组类别数据集
 */
export function searchUserGroupTypeOptions() {
  const options = this.$html.get('user/group/userGroupTypeOptions')
  options
    .then(data => {
      if (data.returnType == 'success') {
        this.userGroupTypeOptions = this.$objTools.dicToArray(data.returnData)
      }
    })
    .catch(() => {
      console.log('error:searchUserGenderOptions')
    })
}

/**
 * 查询用户分组列表信息
 */
export function request() {
  this.tableDataParam.loading = true
  let param = {
    pageSize: this.tableDataParam.paginationData.pageSize,
    pageIndex: this.tableDataParam.paginationData.currentPage,
    filter: this.searchParams,
    sortBy: this.tableDataParam.paginationData.sortItem.prop,
    descending:
      this.tableDataParam.paginationData.sortItem.order == 'descending'
  }
  const options = this.$html.get('user/group/pagedlist', param)
  options
    .then(res => {
      let data = res.returnData
      this.tableDataParam.data = data.list
      this.tableDataParam.paginationData.total = data.total
      this.tableDataParam.loading = false
    })
    .catch(() => {
      this.tableDataParam.loading = false
      console.log('error:user/group/request')
    })
}

export function singleDel(rowObj) {
  if (rowObj.userCount != null) {
    this.$alert(
      '【' + rowObj.userGroupName + '】存在已关联的用户,请取消关联后重试',
      '提示',
      {
        type: 'warning',
        confirmButtonText: '确定'
      }
    )
  } else {
    this.$confirm('确认要删除【' + rowObj.userGroupName + '】吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
      .then(() => {
        const delResult = this.$html.delete(
          'user/group/delUserGroup',
          rowObj.userGroupId
        )
        delResult
          .then(data => {
            if (data.returnType == 'success') {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success'
              })
              this.request()
            }
          })
          .catch(() => {
            console.log('error:user/group/singleDel')
          })
      })
      .catch(() => {
        this.$notify.info({
          title: '消息',
          message: '已取消删除'
        })
      })
  }
}
